package com.luoqiwen.android.sinaurldecoder

import android.content.Intent
import android.net.Uri
import android.view.View
import android.view.View.OnClickListener
import android.widget.EditText
import android.widget.Toast

class ClickBrowseBtnListener(private val main: MainActivity) : OnClickListener {
    private val urlBox = main.findViewById<EditText>(R.id.decodeResult)
    override fun onClick(v: View?) {
        val s = urlBox.text?.toString()
        if (urlBox.text.isNullOrBlank() || s == null || s == "") {
            Toast.makeText(main.applicationContext, "还没有转换哦!", Toast.LENGTH_SHORT).show()
            return
        }
        else {
            main.debug("Button click event detected. Now executing click to open browser task...")
            val uri = Uri.parse(s)
            if (uri == null) {
                Toast.makeText(main.applicationContext, "这个不能识别为URL喔!", Toast.LENGTH_SHORT)
                return
            }
            else {
                val intent = Intent(Intent.ACTION_VIEW, uri)
                Toast.makeText(main.applicationContext, "正在浏览器中打开网页", Toast.LENGTH_SHORT)
                main.startActivity(intent)
            }
        }
    }
}